home *** CD-ROM | disk | FTP | other *** search
/ Java 1996 August / Java - Summer 1996.iso / rockridge / ui / anatomy / example / Converter.java < prev    next >
Encoding:
Java Source  |  1995-11-13  |  5.3 KB  |  206 lines

  1. /* This program could use some layout work, and the functionality
  2.  * could use some tweaking, but it seems to basically work.
  3.  */
  4. import java.awt.*;
  5. import java.util.*;
  6. import java.applet.Applet;
  7.  
  8. public class Converter extends Applet {
  9.     Frame window;
  10.     ConversionPanel metricPanel, usaPanel;
  11.     Unit metricDistances[] = new Unit[3];
  12.     Unit usaDistances[] = new Unit[4];
  13.  
  14.     /** Create the ConversionPanels (one for metric, another for U.S.).
  15.       * I used "U.S." because although Imperial and U.S. distance
  16.       * measurements are the same, this program could be extended to
  17.       * include volume measurements, which aren't the same.
  18.       */
  19.     public void init() {
  20.     setLayout(new GridLayout(2,0,10,10));
  21.  
  22.     metricDistances[0] = new Unit("Centimeters", 0.01);
  23.     metricDistances[1] = new Unit("Meters", 1.0);
  24.     metricDistances[2] = new Unit("Kilometers", 1000.0);
  25.     metricPanel = new ConversionPanel(this, "Metric System",
  26.                         metricDistances);
  27.  
  28.     usaDistances[0] = new Unit("Inches", 0.0254);
  29.     usaDistances[1] = new Unit("Feet", 0.305);
  30.     usaDistances[2] = new Unit("Yards", 0.914);
  31.     usaDistances[3] = new Unit("Miles", 1613.0);
  32.     usaPanel = new ConversionPanel(this, "U.S. System", usaDistances);
  33.  
  34.     add(metricPanel);
  35.     add(usaPanel);
  36.     }
  37.  
  38.     /** Does the conversion from metric to U.S., or vice versa, and
  39.       * updates the appropriate ConversionPanel. */
  40.     void convert(ConversionPanel from) {
  41.     ConversionPanel to;
  42.  
  43.     if (from == metricPanel)
  44.         to = usaPanel;
  45.     else
  46.         to = metricPanel;
  47.     double multiplier = from.getMultiplier() / to.getMultiplier();
  48.     to.setValue(from.getValue() * multiplier);
  49.     }
  50.  
  51.     /** Draws a box around this panel. */
  52.     public void paint(Graphics g) {
  53.     Dimension d = size();
  54.     g.drawRect(0,0, d.width - 1, d.height - 1);
  55.     }
  56.     
  57.     /** Puts a little breathing space between
  58.       * the panel and its contents, which lets us draw a box
  59.       * in the paint() method.
  60.       */
  61.     public Insets insets() {
  62.     return new Insets(3,3,3,3);
  63.     }
  64.  
  65.     public static void main(String args[]) {
  66.     Frame f = new Frame("Converter Applet/Application");
  67.     Converter converter = new Converter();
  68.  
  69.     converter.init();
  70.  
  71.     f.add("Center", converter);
  72.     f.pack();
  73.     f.resize(250, 165);
  74.     f.show();
  75.     }
  76.  
  77. }
  78.  
  79.  
  80. class ConversionPanel extends Panel {
  81.     String title;
  82.     TextField textField;
  83.     Scrollbar slider;
  84.     Choice unitChooser;
  85.     int min = 0;
  86.     int max = 10000;
  87.     Converter controller;
  88.     Unit units[];
  89.  
  90.     //TO DO: Should make both panels' choices the same width.
  91.     ConversionPanel(Converter myController, String myTitle, Unit myUnits[]) {
  92.     super();
  93.     controller = myController;
  94.     title = myTitle;
  95.     units = myUnits;
  96.  
  97.     //Build the left half of this panel.
  98.     textField = new TextField("0", 10);
  99.     slider = new Scrollbar(Scrollbar.HORIZONTAL, 0, 100, min, max);
  100.     Panel leftHalf = new Panel();
  101.     leftHalf.setLayout(new BorderLayout());
  102.     leftHalf.add("Center", textField);
  103.     leftHalf.add("South", slider);
  104.  
  105.     //Build the right half of this panel.
  106.     unitChooser = new Choice();
  107.     for (int i = 0; i < units.length; i++) {
  108.         unitChooser.addItem(units[i].description);
  109.     }
  110.  
  111.     //Put everything in this panel.
  112.     setLayout(new BorderLayout());
  113.     add("North", new Label(title, Label.CENTER));
  114.     add("Center", leftHalf);
  115.     add("East", unitChooser);
  116.     }
  117.  
  118.     /** Returns the multiplier (units/meter) for the currently
  119.       * selected unit of measurement.
  120.       */
  121.     double getMultiplier() {
  122.     int i = unitChooser.getSelectedIndex();
  123.     return (units[i].multiplier);
  124.     }
  125.  
  126.     /** Draws a box around this panel. */
  127.     public void paint(Graphics g) {
  128.     Dimension d = size();
  129.     g.drawRect(0,0, d.width - 1, d.height - 1);
  130.     }
  131.     
  132.     /** Puts a little breathing space between
  133.       * the panel and its contents, which lets us draw a box
  134.       * in the paint() method.
  135.       */
  136.     public Insets insets() {
  137.     // Right offset is more than left, due to Choice bug.
  138.     return new Insets(2,10,2,15);
  139.     }
  140.  
  141.     /** Gets the current value in the text field.
  142.       * That's guaranteed to be the same as the value
  143.       * in the scroller (subject to rounding, of course).
  144.       */
  145.     double getValue() {
  146.     double f;
  147.     try {
  148.         f = Double.valueOf(textField.getText()).doubleValue(); 
  149.     } catch (java.lang.NumberFormatException e) {
  150.         f = 0.0;
  151.     }
  152.     return f;
  153.     }
  154.  
  155.     /** Respond to user actions. */
  156.     public boolean handleEvent(Event e) {
  157.     if (e.target instanceof Scrollbar) {
  158.         textField.setText(String.valueOf(slider.getValue()));
  159.         controller.convert(this);
  160.         return true;
  161.     } else if (e.target instanceof TextField) {
  162.         setSliderValue(getValue());
  163.         controller.convert(this);
  164.         return true;
  165.     } else if (e.target instanceof Choice) {
  166.         controller.convert(this);
  167.         return true;
  168.     } 
  169.     return false;
  170.     }
  171.  
  172.     /** Set the values in the slider and text field. */
  173.     void setValue(double f) {
  174.     setSliderValue(f);
  175.         textField.setText(String.valueOf(f));
  176.     }
  177.  
  178.     /** Set the slider value. */
  179.     void setSliderValue(double f) {
  180.     int sliderValue = (int)f;
  181.  
  182.     if (sliderValue > max)
  183.            sliderValue = max;
  184.     if (sliderValue < min)
  185.         sliderValue = min;
  186.         slider.setValue(sliderValue);
  187.     }
  188. }
  189.  
  190.  
  191. class Unit {
  192.     String description;
  193.     double multiplier;
  194.  
  195.     Unit(String description, double multiplier) {
  196.     super();
  197.     this.description = description;
  198.     this.multiplier = multiplier;
  199.     }
  200.  
  201.     public String toString() {
  202.     String s = "Meters/" + description + " = " + multiplier;
  203.     return s;
  204.     }
  205. }
  206.